/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.Database.ShopUsermenu;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.Message;
import com.lcfgly.Form.Validation.userMenuValidation;
import com.lcfgly.Interface.UserSession;
import com.lcfgly.Service.UserMenuService;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 栏目
 * @author lcfgly
 */
@Controller
@RequestMapping("/control")
public class MenuControl extends FatherController{
    
    @Autowired
    UserMenuService userMenuService;
    
    /**
     * 栏目列表页
     * @param request
     * @param session
     * @return 
     */
    @RequestMapping(value="/menu",method = RequestMethod.GET)
    public String Menu(HttpServletRequest request,@UserSession UserInfo session){
         return "controller/menu";
    }
    
    /**
     * 栏目JSON数据
     * @param request
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/menulist",method = RequestMethod.POST)
    @ResponseBody
    public Map MenuList(HttpServletRequest request,@UserSession UserInfo session)throws ActionException
    {
            return userMenuService.GetMenuListForDataTable(request,session);
    }
    
    /**
     * 栏目添加页
     * @param request
     * @return 
     */
    @RequestMapping(value="/menuadd",method = RequestMethod.GET)
    public String MenuAdd(HttpServletRequest request){
         return "controller/menuadd";
    }
    
    /**
     * 添加栏目
     * @param menu
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value="/menuadd",method = RequestMethod.POST)
    public ModelAndView MenuAdd(@Validated ShopUsermenu menu,BindingResult res,@UserSession UserInfo session) throws ActionException{
        if(res.hasFieldErrors()){
            return new ModelAndView("controller/menuadd","error",getError(res));
        }
        userMenuService.menuadd(menu, session);
        throw new Message("添加成功");
    }
    
    /**
     * 绑定数据验证
     * @param binder 
     */
    @InitBinder
    protected void initbinder(WebDataBinder binder) {
        binder.setValidator(new userMenuValidation());
    }
    
    /**
     * 左侧页面
     * @param request
     * @param session
     * @return 
     */
    @RequestMapping("/left")
    public String left(HttpServletRequest request,@UserSession UserInfo session){
        request.setAttribute("leftmenu", userMenuService.GetSimpleUserMenu(session.getUid()));
        request.setAttribute("producttype", userMenuService.GetProductType());
        request.setAttribute("userinfer", userMenuService.GetSimpleUserInfer(session.getUid()));
        return "controller/left";
    }
   
    /**
     * 栏目删除
     * @param id
     * @param session
     * @return 
     */
    @RequestMapping(value = "/menu/del",method = RequestMethod.POST)
    @ResponseBody
    public Object MenuDel(@RequestParam("id") int id,@UserSession UserInfo session){
        return userMenuService.MenuDelByIdAndUserId(id, session.getUid());
    }
    
    /**
     * 更新页面
     * @param id
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/menuedit/{id}",method = RequestMethod.GET)
    public ModelAndView MenuEdit(@PathVariable("id") int id,@UserSession UserInfo session) throws ActionException{
        return new ModelAndView("controller/menuedit","info",userMenuService.GetMenuById(id, session.getUid()));
    }
    
    /**
     * 更新数据
     * @param usermenu
     * @param res
     * @param session
     * @return
     * @throws ActionException 
     */
    @RequestMapping(value = "/menuedit/{id}",method = RequestMethod.POST)
    public ModelAndView MenuEdit(@Validated ShopUsermenu usermenu,BindingResult res,@UserSession UserInfo session)throws ActionException{
        if(res.hasFieldErrors()){
            ModelAndView result=MenuEdit(usermenu.getId(),session);
            return result.addObject("error", getError(res));
        }
        if(userMenuService.UpdateWithUserId(usermenu,session.getUid()))
            throw new Message("修改成功!");
        else
            throw new ActionException("内容没有更改!");
    }
    
}
